package material.observer;

/**
 * 布告板--天气预报布告板
 * 分析当天的平均气温，最高温度以及最低温度
 */
public class ForecastDisplay implements Observer, DisplayElement {
    private float currentPressure = 29.2f;//当前压力值
    private float lastPressure;//最终压力值
    private Subject weatherData;//由于此布告板实现了Observer接口，所以可以从WeatherData中获取改变

    public ForecastDisplay(Subject weatherData) {
        this.weatherData = weatherData;
        weatherData.registerObserver(this);//构造函数首先将自身添加到观察者行列中
    }

    @Override
    public void display() {
        System.out.print("天气预报: ");
        if (currentPressure > lastPressure) {
            System.out.println("Improving weather on the way!");
        } else if (currentPressure == lastPressure) {
            System.out.println("More of the same");
        } else if (currentPressure < lastPressure) {
            System.out.println("Watch out for cooler, rainy weather");
        }
    }

    @Override
    public void update(float temp, float humidity, float pressure) {
        this.lastPressure = this.currentPressure;
        currentPressure = pressure;
        this.display();
    }
}
